package cn.francis.common.core.model;

import cn.francis.common.enums.OrderEnum;
import cn.francis.common.utils.SqlUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Data;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.StringUtils;
import org.springframework.format.annotation.DateTimeFormat;

import java.io.Serial;
import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * @author 张俊辉
 */

@Data
@Accessors(chain = true)
public class BaseEntity implements Serializable {
    @Serial
    private static final long serialVersionUID = 1L;
    /**
     * 当前记录起始索引 默认值
     */
    public static final int DEFAULT_PAGE_NUM = 1;
    /**
     * 每页显示记录数 默认值 默认查全部
     */
    public static final int DEFAULT_PAGE_SIZE = 100;
    /**
     * 搜索值
     */
    @TableField(exist = false)
    private String searchValue;
    /**
     * 创建者
     */
    @TableField(value = "create_by", fill = FieldFill.INSERT)
    private String createBy;
    /**
     * 创建时间
     */
    @TableField(value = "create_time", fill = FieldFill.INSERT)
    private LocalDateTime createTime;
    /**
     * 更新者
     */
    @TableField(value = "update_by", fill = FieldFill.INSERT_UPDATE)
    private String updateBy;
    /**
     * 更新时间
     */
    @TableField(value = "update_time", fill = FieldFill.INSERT_UPDATE)
    private LocalDateTime updateTime;
    /**
     * 开始时间
     */
    @TableField(exist = false)
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime beginTime;
    /**
     * 结束时间
     */
    @TableField(exist = false)
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime endTime;
    /**
     * 分页大小
     */
    @TableField(exist = false)
    private Integer pageSize;
    /**
     * 当前页数
     */
    @TableField(exist = false)
    private Integer pageNum;
    /**
     * 排序列
     */
    @TableField(exist = false)
    private String orderByColumn;
    /**
     * 排序的方向desc或者asc
     */
    @TableField(exist = false)
    private String isAsc;

    public <T> Page<T> build() {
        Integer pageNumber = ObjectUtil.defaultIfNull(getPageNum(), DEFAULT_PAGE_NUM);
        Integer pageSizeNumber = ObjectUtil.defaultIfNull(getPageSize(), DEFAULT_PAGE_SIZE);
        if (pageNumber <= 0) {
            pageNumber = DEFAULT_PAGE_NUM;
        }

        if (pageSizeNumber > DEFAULT_PAGE_SIZE) {
            pageSizeNumber = DEFAULT_PAGE_SIZE;
        }
        Page<T> page = Page.of(pageNumber, pageSizeNumber);
        OrderItem orderItem = buildOrderItem();
        if (ObjectUtil.isNotNull(orderItem)) {
            page.addOrder(orderItem);
        }
        return page;
    }

    private OrderItem buildOrderItem() {
        // 兼容前端排序类型
        if (OrderEnum.ASCENDING.getName().equals(isAsc)) {
            isAsc = OrderEnum.ASC.getName();
        } else if (OrderEnum.DESCENDING.getName().equals(isAsc)) {
            isAsc = OrderEnum.DESC.getName();
        }
        if (StringUtils.isNotBlank(orderByColumn)) {
            String orderBy = SqlUtil.escapeOrderBySql(orderByColumn);
            orderBy = CharSequenceUtil.toUnderlineCase(orderBy);
            if (OrderEnum.ASC.getName().equals(isAsc)) {
                return OrderItem.asc(orderBy);
            } else if (OrderEnum.DESC.getName().equals(isAsc)) {
                return OrderItem.desc(orderBy);
            }
        }
        return null;
    }
}
